/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.edu.amu.wmi.kino.visualjavafx.visualjavafxgui.editor.visual.widgets;

import java.awt.Dimension;
import java.awt.Rectangle;
import org.netbeans.api.visual.widget.Scene;
import pl.edu.amu.wmi.kino.visualjavafx.model.objects.shapes.Shape;

/**
 *
 * @author psychollek
 */
public class ShapeWidget extends VisualObjectWidget{

    private Shape shape = null;
    private Dimension oldDim = null;
    private boolean oldDimActual = false;

    public ShapeWidget(Scene scene, Shape shape){
        super(scene,shape);
        this.shape = shape;
        initialCalculateClientArea();
    }

    private void initialCalculateClientArea(){
        Rectangle r = new Rectangle();

        r.setRect(-shape.getBorderWidth()/2,
                  -shape.getBorderWidth()/2,
                  shape.getSizeH()+shape.getBorderWidth(),
                  shape.getSizeV()+shape.getBorderWidth());

            oldDim = r.getSize();
            oldDimActual = true;
    }

    @Override
    public Rectangle calculateClientArea(){
        Rectangle r = new Rectangle();

        r.setRect(-shape.getBorderWidth()/2,
                  -shape.getBorderWidth()/2,
                  shape.getSizeH()+shape.getBorderWidth(),
                  shape.getSizeV()+shape.getBorderWidth());

        if(!oldDimActual){
            oldDim = r.getSize();
            oldDimActual = true;
        }

        return r;
    }

    // this is temporary solution to conversion problems.
    @Override
    protected void setModelObjectProperties(){
        
        shape.setHPos(this.getPreferredLocation().getX());
        shape.setVPos(this.getPreferredLocation().getY());
        if(oldDim.height != getPreferredBounds().height || oldDim.width != getPreferredBounds().width ){
            double delta = shape.getBorderWidth();
            shape.setSizeV(this.getPreferredBounds().getHeight()-delta-16);
            shape.setSizeH(this.getPreferredBounds().getWidth()-delta-16);
            oldDimActual = false;
        }

    }

}
